<template>
  <div class="home_page">

    <h2>{{ title }}</h2>

    <div class="article_list">
      <div v-for="user in userList" class="article_card" :key="user.id" >
        <div>
          <div class="thumbnail">
            <img :src="user.avatar"/>
          </div>
          <div class="info">
            <div class="title">昵称：{{ user.nickname }}</div>
            <div class="meta">
              <span>UID：{{ user.id }}</span>
            </div>
            <div class="meta">
              <span>性别：{{ user.sex==0? '♂':'♀'}}</span>
            </div>
            <div class="meta">
              <span>账号：{{ user.username }}</span>
            </div>
            <div class="meta">
              <span>邮箱：{{ user.email }}</span>
            </div>
            <div class="btnList">
              <button @click="handleFocus(user.id)">关注用户</button>
              <button @click="handleFriend(user.id)">添加好友</button>
            </div>
            <div class="btnList">
              <button @click="handleChat(user.id)">进行对话</button>
            </div>
          </div>
        </div>
      </div>
    </div>

    <h2>{{ userList.length==0? '未找到数据' : '' }}</h2>

  </div>
</template>
    










<script setup>
import { emitter } from '@/router/eventBus';
import { onMounted, ref } from 'vue'
import hahaPng from '@/assets/xixi.png'
import { focusUserById } from '@/api/BlogUser';
import server from '@/api/server';
import { onUnmounted } from 'vue';
import { addFriend } from '@/api/BlogUser';
import { userDialogue } from '@/api/BlogUser';
import { useRouter } from 'vue-router';

const router = useRouter();

const title = ref("根据用户昵称搜索：");

const userList = ref([{
  id: 1,
  avatar: hahaPng,
  email: 1234,
  nickname: 'hahahaha',
  phone: 999,
  username: 999,
  sex: 0
}])

const handleFocus = (focusId)=>{
  const params = {
    focusId: focusId
  }
  try {
    focusUserById(server.token, params).then((res)=>{
      if (res.data.code==0) {
        alert("关注成功："+ res.data.data)
      } else {
        alert("关注失败："+ res.data.msg)
      }
    })
  } catch (error) {
    alert("关注失败："+ error)
  }
}

const handleFriend = (friendId)=>{
  const params = {
    friendId: friendId
  }
  try {
    addFriend(server.token, params).then((res)=>{
      if (res.data.code==0) {
        alert("添加成功："+ res.data.data)
      } else {
        alert("添加失败："+ res.data.msg)
      }
    })
  } catch (error) {
    alert("添加失败："+ error)
  }
}

onMounted(()=>{
  displayData()
  emitter.on('user_searched', displayData);
})

onUnmounted(()=>{
  emitter.off('user_searched', displayData)
})

function displayData() {
  title.value = server.searchMsg
  userList.value = server.searchData.data
}

const handleChat = (chatUserId)=>{
  const params = {
    targetUserId: chatUserId
  }
  try {
    userDialogue(server.token, params).then((res)=>{
      if (res.data.code == 0) {
        router.push('/userDialogue')
      } else {
        alert("获取聊天记录失败："+res.data.msg)
      }
    })
  } catch (error) {
        alert("获取聊天记录失败："+error)
  }
}
</script>











  
  
  
<style scoped>
.btnList{
  display: flex;flex-flow: row nowrap;
  justify-content: start;
  margin-top: 10px;
}
.btnList button{
  display: block;  margin-left: 25px;
  width: 70px;height: 30px;
  

  background-color: #ff6699;
  border: none;
  color: white;
  border-radius: 4px;
}

.home_page {
  width: 1000px;
  margin: 0 auto;padding: 25px;
  background: #f4f4f4;
}

.tagbox {
  display: flex;flex-flow:row wrap;
}

.tagbox button{
  display: flex;flex-flow:row wrap;
  justify-content: center;
  width: 70px;height: 40px;

  align-items: center;
  background: #f3f4f6;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  transition: background 0.2s;
}

.tagbox button:hover{
  background: #e5e7eb;
}













/* 文章列表 */
.article_list {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin: 20px 0vw 120px;
}
/* 文章卡片 */
.article_card {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 0 6px rgba(0,0,0,0.05);
  transition: transform 0.2s;
}

.article_card:hover {
  transform: translateY(-4px);
}

/* 缩略图 */
.thumbnail {
  height: 180px;
  overflow: hidden;
  position: relative;
}

.thumbnail img {
  height: 100%;width: 100%;
  object-fit: cover;
}

/* 文章信息 */
.info {
  padding: 8px 10px;
}

.info .title {
  font-size: 14px;
  font-weight: bold;
  margin-bottom: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.info .meta {
  font-size: 12px;
  color: #999;
}
</style>